<?php
/**
 * Created by PhpStorm.
 * User: yuanxr
 * Date: 2018/6/10
 * Time: 上午8:33
 */
namespace Utility\Convert;
use Utility\Extend\EString;
class Xml
{
    private $_EString;

    function __construct(){
        $this->_EString = new EString();
    }
    /**
     * enarray
     *
     * @param string $src 需要转换为数组的xml原始字符串
     * @return array 返回已转换好的数组
     */
    public function enarray(string $src)
    {
        return (array)new SimpleXMLElement($this->_EString->trim_inner($src), LIBXML_NOCDATA);
    }

    /**
     * dearray
     *
     * @param array $src 需要转换为xml字符串的原始数组
     * @return string 转换好的xml字符串
     */
    public function dearray(array $src)
    {
        $result = '<xml>';
        foreach ($src as $k=>$v) {
            $result .= "<${k}>";
            $result .= "<![CDATA[${v}]]>";
            $result .= "</${k}>";
        }
        $result .= '</xml>';
        return $result;
    }
}
